// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <melon/container/f14_map.h>
#include <libstemmer.h> // @manual

#include <pollux/functions/udf.h>
#include <pollux/common/strings/string_impl.h>

namespace kumo::pollux::functions {
    namespace detail {
        // Wrap the sbstemmer library and use its sb_stemmer_stem
        // to get word stem.
        class Stemmer {
        public:
            Stemmer(sb_stemmer *stemmer) : sbStemmer_(stemmer) {
                POLLUX_CHECK_NOT_NULL(stemmer);
            }

            ~Stemmer() {
                sb_stemmer_delete(sbStemmer_);
            }

            // Get the word stem or NULL if out of memory.
            const char *stem(const std::string &input) {
                return (const char *) (sb_stemmer_stem(
                    sbStemmer_,
                    reinterpret_cast<unsigned char const *>(input.c_str()),
                    input.length()));
            }

        private:
            sb_stemmer *sbStemmer_;
        };
    } // namespace detail

    /// word_stem function
    /// word_stem(word) -> varchar
    ///     return the stem of the word in the English language
    /// word_stem(word, lang) -> varchar
    ///     return the stem of the word in the specificed language
    ///
    /// Use the snowball stemmer library to calculate the stem.
    /// https://snowballstem.org
    /// The website provides Java implementation which is used in Presto as well
    /// as C implementation. Therefore, both Presto and Prestimissio
    /// would have the same word stem results.
    template<typename TExec>
    struct WordStemFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        // ASCII input always produces ASCII result.
        static constexpr bool is_default_ascii_behavior = true;

        MELON_ALWAYS_INLINE void call(
            out_type<Varchar> &result,
            const arg_type<Varchar> &input) {
            return doCall<false>(result, input);
        }

        MELON_ALWAYS_INLINE void callAscii(
            out_type<Varchar> &result,
            const arg_type<Varchar> &input) {
            return doCall<true>(result, input);
        }

        MELON_ALWAYS_INLINE void call(
            out_type<Varchar> &result,
            const arg_type<Varchar> &input,
            const arg_type<Varchar> &lang) {
            return doCall<false>(result, input, lang);
        }

        MELON_ALWAYS_INLINE void callAscii(
            out_type<Varchar> &result,
            const arg_type<Varchar> &input,
            const arg_type<Varchar> &lang) {
            return doCall<true>(result, input, lang);
        }

        template<bool isAscii>
        MELON_ALWAYS_INLINE void doCall(
            out_type<Varchar> &result,
            const arg_type<Varchar> &input,
            const std::string &lang = "en") {
            auto *stemmer = getStemmer(lang);
            POLLUX_USER_CHECK_NOT_NULL(
                stemmer, "Unsupported stemmer language: {}", lang);

            std::string lowerOutput;
            stringImpl::lower<isAscii>(lowerOutput, input);
            auto *stem = stemmer->stem(lowerOutput);
            POLLUX_CHECK_NOT_NULL(
                stem, "Stemmer library returned a NULL (out-of-memory)");
            result = stem;
        }

    private:
        melon::F14FastMap<std::string, std::unique_ptr<detail::Stemmer> > stemmers_;

        // Get a detail::Stemmer from the the map using the lang as the key or create
        // a new one if it doesn't exist. Return nullptr if the specified lang is not
        // supported.
        detail::Stemmer *getStemmer(const std::string &lang) {
            if (auto found = stemmers_.find(lang); found != stemmers_.end()) {
                return found->second.get();
            }
            // Only support ASCII and UTF-8.
            if (auto sbStemmer = sb_stemmer_new(lang.c_str(), "UTF_8")) {
                auto *stemmer = new detail::Stemmer(sbStemmer);
                stemmers_[lang] = std::unique_ptr<detail::Stemmer>(stemmer);
                return stemmer;
            }
            return nullptr;
        }
    };
} // namespace kumo::pollux::functions
